![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
Self-TestProgram Bad6 violates several of the guidelines presented in this chapter. This program loads data type information from a data file and displays a summary as shown in Figure 6.1. The following code shows how program Bad6 works. Appendix A, Self-Test Solutions, contains an improved version of this code. This is not the only possible solution. You can download updated bad and good versions of this program from the Web at Note: In the following code, the line If Len(token) > 0 Then output_txt = output_txt & Format(token, FORMAT_SPECIFIER) should extend as far as it can and then be truncated at the right edge of the page. It should not be wrapped. Do not extend it beyond the margin or anything fancy like that. Just chop it off, in mid-word if thats where the page ends (that would make the best example). It is demonstrating code that does not wrap.
Option Explicit
Make txtOutput as large as possible.
Private Sub Form_Resize()
txtOutput.Move 0, 0, ScaleWidth, ScaleHeight
End Sub
Read the data from the data file.
Private Sub Form_Load()
Dim fnum As Integer
Open the file.
fnum = FreeFile
Open App.Path & \types.dat For Input As fnum
Read the data.
LoadData fnum
End Sub
Read the data file and close it.
Sub LoadData(fnum As Integer)
Const FORMAT_SPECIFIER = !@@@@@@@@@@@@@@@@@@@@
Dim output_txt As String
Dim all_data As String
Dim next_line As String
Dim token As String
Read all the data.
all_data = Input(LOF(fnum), #fnum)
Start with column headers.
output_txt = _
Format(Data Type, FORMAT_SPECIFIER) & _
Format(Size, FORMAT_SPECIFIER) & _
Format(Approx Min, FORMAT_SPECIFIER) & _
Format(Approx Max, FORMAT_SPECIFIER)
output_txt = output_txt & vbCrLf & _
Format(---------, FORMAT_SPECIFIER) & _
Format(----, FORMAT_SPECIFIER) & _
Format(----------, FORMAT_SPECIFIER) & _
Format(----------, FORMAT_SPECIFIER) & _
vbCrLf
Read the lines from the file.
Do While Len(all_data) > 0
Get the next line.
next_line = GetToken(all_data, vbCrLf)
Break the line into pieces.
Do While Len(next_line) > 0
token = GetToken(next_line, ;)
If Len(token) > 0 Then output_txt = output_txt & Format(token, FORMAT_SPECIFIER)
Loop
output_txt = output_txt & vbCrLf
Loop
Display the result.
txtOutput = output_txt
End Sub
Get the next delimited token from the string txt.
Function GetToken(txt As String, delimiter As String) As String
Dim p As Integer
Find the delimiter.
p = InStr(txt, delimiter)
Get the token.
If p = 0 Then p = Len(txt) + 1
GetToken = Left(txt, p - 1)
See whats left of the string txt.
p = Len(txt) - p + 1 - Len(delimiter)
If p <= 0 Then
txt =
Else
txt = Right(txt, p)
End If
End Function
SummaryMaintainable code must be easy to understand. When someone later reads the code, any extra time spent struggling to make sense out of trivial details like variable names and scope is time that could be better spent studying the programs structure. Make your code easy to understand by using the techniques summarized in the following Bug Stoppers.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|